package bm25;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class dd {

    public static void main(String[] args) throws Exception {
        test();
    }

    public static void test() throws Exception {
        //Directory dir = new RAMDirectory();
        Directory dir = FSDirectory.open(Paths.get("/storage/huangtiantian/index"));
        //IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47,
        //new StandardAnalyzer(Version.LUCENE_47));
        Analyzer analyzer = new StandardAnalyzer();


        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        config.setSimilarity(new BM25Similarity());
        IndexWriter writer = new IndexWriter(dir, config);

        writer.commit();


        Document doc = null;
        File f = new File("/storage/huangtiantian/trec");
        for (File file:f.listFiles()){
            doc = new Document();
            doc.add(new TextField("content","content ,,,.....",Store.YES));
            doc.add(new StringField("fileName",file.getName(),Store.YES));
            //doc.add(new Field("path",file.getAbsolutePath(),TextField.TYPE_STORED));

            writer.addDocument(doc);
        }

        writer.close();

        /*
         * 新建一个文件存结果
         */
        File destFile = new File("/storage/huangtiantian/result/BM25_list1.txt");
        if (!destFile.exists()) {
            destFile.createNewFile();
        }


        /*
         * 在这里建立循环
         */
        System.out.println("读文件");
        String fileName = "/storage/huangtiantian/topic/topicA.txt";
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            //System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            String[] arrs = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println(line );
                arrs=tempString.split("\t");
                System.out.println(arrs[3]);
                line++;

                BufferedWriter bw = new BufferedWriter(new FileWriter(destFile,true));
                bw.write(arrs[0] + "\t" + arrs[3]+ "\t");
                bw.newLine();
                //bw.write("--------------------");
                bw.close();

                dd.bm25Similarity(dir, arrs[3], analyzer);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

    }


    public static void bm25Similarity(Directory dir,String str,Analyzer analyzer) throws IOException{
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);
        //specify the BM25 similarity measures
        searcher.setSimilarity(new BM25Similarity());

        //String str = "bc";
        //System.out.println(str);
        try {
            //Analyzer analyzer = new StandardAnalyzer();
            QueryParser parser = new QueryParser("content",analyzer);
            Query q = parser.parse(str);
            //System.out.println(str+"77777777777");
            //System.out.println(("A 44-year-old man with coffee-ground emesis, tachycardia, hypoxia, hypotension and cool, clammy extremities"));

            //TermQuery q = new TermQuery(new Term("content", str));

            TopDocs topdocs = searcher.search(q, 800000);
            ScoreDoc[] scoreDocs = topdocs.scoreDocs;



            Map<String, Float> map = new HashMap<String, Float>();

            for (ScoreDoc sd : scoreDocs) {
                //7、根据searcher和ScoreDoc获取具体的document对象
                Document d = searcher.doc(sd.doc);
                //8、根据document对象获取需要的值
                System.out.println(d.get("fileName") +" " +sd.score);
                //System.out.println("bookname====" +  d.get("bookname"));
                //System.out.println(searcher.explain(q, sd.doc));
                System.out.println("----------------");
                //System.out.println(sd.score);
                map.put(d.get("fileName"), sd.score);

            }
            reader.close();
            System.out.println(map);
            //System.out.println(list);
            //BufferedWriter bw = new BufferedWriter(new FileWriter("D:/javacode2016/helloLucene/result/BM25_list.txt",true));
            try {
                String line = System.getProperty("line.separator");
                StringBuffer strr = new StringBuffer();
                FileWriter fw = new FileWriter("/storage/huangtiantian/result/BM25_list1.txt", true);
                Set set = map.entrySet();
                Iterator iter = set.iterator();
                while(iter.hasNext()){
                    Map.Entry entry = (Map.Entry)iter.next();
                    strr.append(entry.getKey()+" : "+entry.getValue()).append(line);
                }
                fw.write(strr.toString());
                fw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            //bw.newLine();
            // bw.close();

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("ooooooooooooooooooooo");
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        reader.close();
    }



}